package com.example.websocket.service.impl;

import com.example.websocket.mapper.PyqMapper;
import com.example.websocket.pojo.CommentPo;
import com.example.websocket.pojo.LikePo;
import com.example.websocket.pojo.Pyq;
import com.example.websocket.pojo.dto.CommentDto;
import com.example.websocket.pojo.dto.LikeDto;
import com.example.websocket.pojo.dto.PublishPyqDto;
import com.example.websocket.service.PyqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ author 韩
 * time: 2024/3/8 11:48
 */
@Service
public class PyqServiceImpl implements PyqService {

    @Autowired
    private PyqMapper pyqMapper;
    @Override
    public List<Pyq> getPyqList() {

        List<Pyq> pyqList = pyqMapper.getPyqList();
        pyqList.forEach(
                (v) -> {
                    Integer id = v.getId();
                    List<LikePo> likeList = pyqMapper.getLikeList(id);
                    List<CommentPo> commentList = pyqMapper.getCommentList(id);
                    v.setLikes(likeList);
                    v.setComments(commentList);
                }
        );
        return pyqList;
    }

    @Override
    public boolean addLike(LikeDto likeDto) {
        return pyqMapper.addLike(likeDto.getPyqId(), likeDto.getUsername());
    }

    @Override
    public boolean deleteLike(LikeDto likeDto) {
        return pyqMapper.deleteLike(likeDto.getPyqId(), likeDto.getUsername());
    }

    @Override
    public boolean addComment(CommentDto commentDto) {
        return pyqMapper.addComment(commentDto.getPyqId(), commentDto.getUsername(),commentDto.getContent());

    }

    @Override
    public Boolean publishPyq(PublishPyqDto dto) {
        return pyqMapper.publishPyq(dto.getUsername(),dto.getContent());
    }


}
